package com.gs.wechatdemo.adapter.base;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

public abstract class BasePagerAdapter<T> extends PagerAdapter {

    protected Activity activity;
    private Context context;
    protected List<T> mData;
    private int layoutId;
    private View convertView;

    public BasePagerAdapter(Activity activity, Context context, List<T> mData, int layoutId) {
        this.activity = activity;
        this.context = context;
        this.mData = mData;
        this.layoutId = layoutId;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        convertView = LayoutInflater.from(context).inflate(layoutId, null);
        return convert(container, mData.get(position), position);
    }

    public abstract Object convert(ViewGroup container, T bean, int position);

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    public <V extends View> V fd(int id) {
        return convertView.findViewById(id);
    }

    protected OnItemLongClickListener onItemLongClickListener;

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemLongClickListener {
        void onLongClickListener(View v, int position);
    }

    protected OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onClickListener(View v, int position);
    }
}
